#ifndef SLIST_H
#define SLIST_H

// Singly linked list struct definition.
typedef struct sList {
	struct sList* next;
	void* data;
} sList;

// Adds the void* to the end of the sList.
static inline void sListAdd(sList** head, void* data) {
	sList* node = new sList;
	node->next = NULL;
	node->data = data;
	for(sList* n = *head; n != NULL; n = n->next) {
		if(n->next == NULL) {
			n->next = node;
			return;
		}
	}
	*head = node; // This code is only reached if the sList is empty. (*head == NULL)
}

// Returns the void* from the start of the sList.
static inline void* sListFetch(sList** head) {
	if(*head == NULL) return NULL;
	sList* node = *head;

	*head = node->next;
	void* data = node->data;

	delete node;

	return data;
}

/*
Example usage:

	sList* head = NULL; // define our head variable and initialize it to NULL. This is our list.

	int data[] = { 0,1,2,3,4 };
	for(int i = 0; i<5; i++) {
		sListAdd(&head, (void*)&data[i]); // Add pointers to our numbers 0,1,2,3,4 to the list.
	}

	for(int i = 0; i<5; i++) {
		int* i = (int*)sListFetch(&head); // Fetch the pointers back.
		cout << i; // And print them one by one.
	}

Result is:
01234
*/


#endif // SLIST_H
